package bpm.dao.impl.user;

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import bpm.dao.user.OrgTrModuleDao;
import bpm.domain.user.OrgTrModule;
/**
 * 
 * @author ZhaoWangLong Jul 15, 2011
 *
 */
@Repository("orgTrModuleDao")
public class OrgTrModuleDaoImpl implements OrgTrModuleDao {
	@Resource
	private JdbcTemplate jdbcTemplate;
	private Logger logger=Logger.getLogger(OrgTrModuleDaoImpl.class);
	public void delete(String moduleId) {
		String sql="delete from ORG_TR_MODULE where MAIN_ID=?";
		logger.debug("DeleteSQL:"+sql);
		this.jdbcTemplate.update(sql,new Object[]{moduleId});
	}

	public void delete(String[] moduleIds) {
		StringBuffer sql=new StringBuffer();
		sql.append("delete from ORG_TR_MODULE where MAIN_ID in ( ");
		for (int i = 0; i < moduleIds.length; i++) {
			if(i!= moduleIds.length-1){
				sql.append("'").append(moduleIds[i]).append("',");
			}else{
				sql.append("'").append(moduleIds[i]).append("' )");
			}
		}
		logger.debug("DeleteSQL:"+sql.toString());
		this.jdbcTemplate.update(sql.toString());
	}
	@SuppressWarnings("unchecked")
	public List<OrgTrModule> findAll() {
		String sql="select MAIN_ID, APP_ID, MODULE_CODE, MODULE_NAME, MODULE_REMARK, "
			      +"ORDER_NO, MODULE_PID, ACTION_URL, TARGET, ICON_URL  from ORG_TR_MODULE";
		return this.jdbcTemplate.query(sql, new OrgTrModuleRowMapper());
	}
	@SuppressWarnings("unchecked")
	public List<OrgTrModule> findModuleByPid(String pid) {
	StringBuffer sql=new StringBuffer();
	sql.append("with module(MAIN_ID, APP_ID, MODULE_CODE, MODULE_NAME, MODULE_REMARK, ORDER_NO, MODULE_PID, ACTION_URL, TARGET, ICON_URL  ) as ")
		.append("(select MAIN_ID, APP_ID, MODULE_CODE, MODULE_NAME, MODULE_REMARK, ")
		.append(" ORDER_NO, MODULE_PID, ACTION_URL, TARGET, ICON_URL  from ORG_TR_MODULE where MODULE_PID=? ")
		.append(" union all ")
		.append(" select a.MAIN_ID, a.APP_ID, a.MODULE_CODE, a.MODULE_NAME, a.MODULE_REMARK, ")
		.append("   a.ORDER_NO, a.MODULE_PID, a.ACTION_URL, a.TARGET, a.ICON_URL  from ORG_TR_MODULE  as a,module as b  ")
		.append(" where a.MODULE_PID=b.MAIN_ID   ), module2(MAIN_ID, APP_ID, MODULE_CODE, MODULE_NAME, MODULE_REMARK, ORDER_NO, MODULE_PID, ACTION_URL, TARGET, ICON_URL  ) as")
		.append(" (select MAIN_ID, APP_ID, MODULE_CODE, MODULE_NAME, MODULE_REMARK,ORDER_NO, MODULE_PID, ACTION_URL, TARGET, ICON_URL from ORG_TR_MODULE where MAIN_ID=? union all ")
		.append(" select MAIN_ID, APP_ID, MODULE_CODE, MODULE_NAME, MODULE_REMARK,ORDER_NO, MODULE_PID, ACTION_URL, TARGET, ICON_URL from module ) ")
		.append(" select MAIN_ID, APP_ID, MODULE_CODE, MODULE_NAME, MODULE_REMARK,  ")
		.append(" ORDER_NO, MODULE_PID, ACTION_URL, TARGET, ICON_URL from module2 order by MODULE_PID asc ,order_no asc ");
		logger.debug("WithSQL:"+sql.toString());
		return this.jdbcTemplate.query(sql.toString(), new Object[]{pid,pid},new OrgTrModuleRowMapper() );
	}
	@SuppressWarnings("unchecked")
	public List<OrgTrModule> findModuleByRole(String roleId) {
		StringBuffer sql=new StringBuffer();
		sql.append("select MAIN_ID, APP_ID, MODULE_CODE, MODULE_NAME, MODULE_REMARK, ")
			.append(" ORDER_NO, MODULE_PID, ACTION_URL, TARGET, ICON_URL  from ORG_TR_MODULE where ")
			.append(" MAIN_ID in ( select MODULE_ID from ORG_TR_GROUPMODULE where GROUP_ID =? ) ");
		logger.debug("QuerySQL:"+sql.toString());
		return this.jdbcTemplate.query(sql.toString(), new Object[]{roleId},new OrgTrModuleRowMapper());
	}

	public void insert(OrgTrModule orgTrModule) {
		StringBuffer sql=new StringBuffer();
		sql.append(" insert into ORG_TR_MODULE(MAIN_ID, APP_ID, MODULE_CODE, MODULE_NAME, MODULE_REMARK, ")
			.append("ORDER_NO, MODULE_PID, ACTION_URL, TARGET, ICON_URL) values  ( ?,?,?,?,?,?,?,?,?,?)");
		logger.debug("InsertSQL:"+sql.toString());
		this.jdbcTemplate.update(sql.toString(),new Object[]{
			orgTrModule.getMainId(),orgTrModule.getAppId(),orgTrModule.getModuleCode(),orgTrModule.getModuleName(),
			orgTrModule.getModuleRemark(),orgTrModule.getOrderNo(),
			orgTrModule.getPModule()==null?null:orgTrModule.getPModule().getMainId(),
			orgTrModule.getActionUrl(),orgTrModule.getTarget(),orgTrModule.getIconUrl()
		});
	}

	public void update(OrgTrModule orgTrModule) {
		StringBuffer sql=new StringBuffer();
		sql.append("update ORG_TR_MODULE set APP_ID=?,MODULE_CODE=?,MODULE_NAME=?,MODULE_REMARK=?,")
		.append("ORDER_NO=?,MODULE_PID=?,ACTION_URL=?,TARGET=?,ICON_URL=? where MAIN_ID=?");
		logger.debug("UpdateSQL:"+sql.toString());
		this.jdbcTemplate.update(sql.toString(),new Object[]{
			orgTrModule.getAppId(),orgTrModule.getModuleCode(),orgTrModule.getModuleName(),
			orgTrModule.getModuleRemark(),orgTrModule.getOrderNo(),
			orgTrModule.getPModule()==null?null:orgTrModule.getPModule().getMainId(),
			orgTrModule.getActionUrl(),orgTrModule.getTarget(),orgTrModule.getIconUrl(),
			orgTrModule.getMainId()
		});
	}
	public void update(String roleId, String[] moduleIds) {
		String delete="delete from ORG_TR_GROUPMODULE where GROUP_ID=?";
		logger.debug("DeleteSQL:"+delete);	
		this.jdbcTemplate.update(delete,new Object[]{roleId});
		if(moduleIds!=null&&moduleIds.length>0){
			StringBuffer insert=new StringBuffer();
			insert.append("insert into ORG_TR_GROUPMODULE (MAIN_ID, GROUP_ID, MODULE_ID) values ");
			for (int i = 0; i < moduleIds.length; i++) {
				if(i!=moduleIds.length-1){
					insert.append(" ( '").append(roleId+"_"+moduleIds[i]).append("','")
						  .append(roleId).append("','").append(moduleIds[i]).append("'),");
				}else{
					insert.append(" ( '").append(roleId+"_"+moduleIds[i]).append("','")
					  .append(roleId).append("','").append(moduleIds[i]).append("')");
				}
			}
			logger.debug("InsertSQL:"+insert.toString());	
			this.jdbcTemplate.update(insert.toString());
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<OrgTrModule> findModulesByUserId(String userId) {
		StringBuffer sql=new StringBuffer();
		sql.append("select MAIN_ID, APP_ID, MODULE_CODE, MODULE_NAME, MODULE_REMARK, ")
			.append(" ORDER_NO, MODULE_PID, ACTION_URL, TARGET, ICON_URL  from ORG_TR_MODULE where ")
			.append(" MAIN_ID in ( select MODULE_ID from ORG_TR_GROUPMODULE where GROUP_ID in ( ")
			.append(" select GROUPID from ORG_GROUPUSER where USERID=?))")
			.append(" order by ORDER_NO asc");
		return this.jdbcTemplate.query(sql.toString(), new Object[]{userId},new OrgTrModuleRowMapper());
	}
}
